(load "auctex.el" nil t t)
(load "preview-latex.el" nil t t)
(require 'cdlatex)
(require 'zotelo)
(setq TeX-auto-save t)
(setq TeX-parse-self t)
(setq-default TeX-master nil)
(add-hook 'LaTeX-mode-hook 'turn-on-reftex) 
(add-hook 'LaTeX-mode-hook 'turn-on-cdlatex)
(add-hook 'TeXinfo-mode-hook 'outline-minor-mode)
(add-hook 'LaTeX-mode-hook 'outline-minor-mode)
(add-hook 'LaTeX-mode-hook 'zotelo-minor-mode)
(add-hook 'org-mode-hook 'zotelo-minor-mode)
(setq zotelo-default-translator 'BibLaTeX-cite)
(put 'zotero-collection 'safe-local-variable
     '(lambda (x)
	(stringp x)))
(setq preview-auto-cache-preamble t)
(setq TeX-outline-extra
            '(("[ \t]*\\\\\\(bib\\)?frametitle\\b" 8)
         ("\\\\bibliography\\b" 2)))
(setq reftex-ref-macro-prompt nil)
(setq LaTeX-math-abbrev-prefix (kbd "\C-c@"))

;; Forward search for pdf file
(setq TeX-source-correlate-mode t) 
;; (setq TeX-source-correlate-start-server t)
(setq TeX-source-correlate-method 'synctex)
(setq TeX-PDF-mode t)
(add-hook 'LaTeX-mode-hook
            '(lambda ()
                 (add-to-list 'TeX-expand-list
                                  '("%u" Okular-make-url))))
(defun Okular-make-url ()
  (concat
   "file://"
   (expand-file-name (funcall file (TeX-output-extension) t)
                          (file-name-directory (TeX-master-file)))
   "#src:"
   (TeX-current-line)
   (expand-file-name (TeX-master-directory))
   "./"
   (TeX-current-file-name-master-relative)))
(setq TeX-view-program-list
      '(("Okular"
       ("okular --unique %u")))) ;; <<-- This is part of the quick-fix.
                                 ;; If all is in sync again, one can
                                 ;; hopefully use the regular method
                                 ;; from the next line again.
(setq TeX-view-program-selection '((output-pdf "Okular")))


(setq TeX-command-list
'(("TeX" "latex %s.tex" TeX-run-TeX nil (plain-tex-mode))
;; ("LaTeX" "xelatex -output-directory=./output %s.tex" TeX-run-TeX nil (latex-mode))
("AnLaTeX" "xelatex -output-directory=./output %s.tex" TeX-run-TeX nil (latex-mode))
;; ("LaTeX" "xelatex  %s.tex" TeX-run-TeX nil (latex-mode))
("LaTeX" "xelatex -shell-escape %s.tex" TeX-run-TeX nil (latex-mode))
("DFLaTeX" "%`%l%(mode)%' %t" TeX-run-TeX nil  (latex-mode doctex-mode)  :help "Run LaTeX")
;; Inverse search mode compile
("sync" "xelatex -shell-escape --synctex=1 %s.tex" TeX-run-TeX nil (latex-mode))
("fmf" "fmf.sh" TeX-run-TeX nil (latex-mode))
("Dvips" "dvips %d -o %f " TeX-run-command nil t)
("ps2pdf" "ps2pdf %f %s.pdf" TeX-run-command nil t)
;; ("DFLaTeX" "pdflatex %s.tex" TeX-run-TeX nil (latex-mode))
;; ("View" "okular %s.pdf" TeX-run-command nil t) ;TeX-run-discard
("View" "%V" TeX-run-discard-or-function t t :help "Run Viewer")
("gv" "gv %f" TeX-run-command nil t)
("Xpdf" "xpdf %s.pdf" TeX-run-command nil t)
("ConTeXt" "texexec --once --nonstop --texutil %t" TeX-run-TeX nil (context-mode))
("ConTeXt PDF" "texexec --pdf %t" TeX-run-TeX nil (context-mode))
("ConTeXt Interactive" "texexec --once --texutil %t" TeX-run-interactive t (context-mode))
("ConTeXt Full" "texexec %t" TeX-run-interactive nil (context-mode))
("ConTeXt Clean" "texutil --purgeall" TeX-run-interactive nil (context-mode))
("BibTeX" "bibtex %s" TeX-run-BibTeX nil t)
("Index" "makeindex %s" TeX-run-command nil t)
("Check" "lacheck %s" TeX-run-compile nil t)
("gbk2uni" "gbk2uni %s.out" TeX-run-command nil t)
("Clean" "TeX-clean" TeX-run-function nil t :help "Delete generated intermediate files")
("Clean All" "(TeX-clean t)" TeX-run-function nil t :help "Delete generated intermediate and output files")
))

(autoload 'turn-on-bib-cite "bib-cite")
(add-hook 'LaTeX-mode-hook 'turn-on-bib-cite)
;;; end for bib-cite;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(setq reftex-default-bibliography
      (quote
       ("~/emacs/latex/bib/test.bib")
       ))

(setq reftex-toc-split-windows-horizontally t)
(setq reftex-toc-split-windows-fraction 0.2)

(add-hook 'reftex-toc-mode-hook
	  '(lambda ()
	     (define-key reftex-toc-map "j" 'reftex-toc-next)
	     (define-key reftex-toc-map "k" 'reftex-toc-previous)
	     (define-key reftex-toc-map "n" 'reftex-toc-next-heading)
	     (define-key reftex-toc-map "p" 'reftex-toc-previous-heading)
	     ))
(add-hook 'reftex-select-label-mode-hook
	  '(lambda ()
	     (define-key reftex-select-label-mode-map "j" 'reftex-select-next)
	     (define-key reftex-select-label-mode-map "k" 'reftex-select-previous)
	     (define-key reftex-select-label-mode-map "n" 'reftex-select-next-heading)
	     (define-key reftex-select-label-mode-map "p" 'reftex-select-previous-heading)
	     ))

(add-hook 'reftex-select-bib-mode-hook
	  '(lambda ()
	     (define-key reftex-select-bib-mode-map "j" 'reftex-select-next)
	     (define-key reftex-select-bib-mode-map "k" 'reftex-select-previous)
	     ))

(add-hook 'LaTeX-mode-hook
	  '(lambda ()
	     (define-key LaTeX-mode-map (kbd "M-e") 'preview-environment)
	     (define-key LaTeX-mode-map (kbd "M-s") 'preview-section)
	     (define-key LaTeX-mode-map (kbd "C-M-e") 'preview-clearout-at-point)
	     (define-key LaTeX-mode-map (kbd "C-.") 'reftex-toc-toggle-context)
	     (define-key LaTeX-mode-map (kbd "M-n") 'outline-next-heading)
	     (define-key LaTeX-mode-map (kbd "M-p") 'outline-previous-heading)
	     (define-key LaTeX-mode-map (kbd "\C-ct") 'hide-other)
	     (define-key LaTeX-mode-map (kbd "\C-ch") 'hide-body)
	     (define-key LaTeX-mode-map (kbd "\C-ck") 'hide-subtree)
	     (define-key LaTeX-mode-map (kbd "\C-cj") 'show-entry)
	     (define-key LaTeX-mode-map (kbd "\C-cs") 'show-all)
	     (define-key LaTeX-mode-map (kbd "\C-co") 'show-branches)
	     (define-key LaTeX-mode-map (kbd "\C-o")  'outline-toggle-children)
	     (define-key LaTeX-mode-map (kbd "\C-c)")     'LaTeX-close-environment)
	     (define-key LaTeX-mode-map (kbd "<f11>") 'beamer-frame-compile)
	     (define-key LaTeX-mode-map (kbd "`") 'xwp-exchange-semicolon-grave)
	     ))
(defun xwp-exchange-semicolon-grave ()
  (interactive)
  (insert ";"))
  
(add-hook 'reftex-mode-hook
	  '(lambda ()
	     (define-key reftex-mode-map (kbd "\C-c[")   'reftex-label)
	     (define-key reftex-mode-map (kbd "\C-c]")    'reftex-reference)
	     (define-key reftex-mode-map (kbd "\C-c\\")    'reftex-citation)
	     ))


(setq preview-scale-function 1.5)
(defun latex-word-count ()
  (interactive)
  (shell-command (concat "texcount -inc -total "
                         ; "uncomment then options go here "
                         (buffer-file-name))))

(defun yas/advise-indent-function (function-symbol)
  (eval `(defadvice ,function-symbol (around yas/try-expand-first activate)
           ,(format
             "Try to expand a snippet before point, then call `%s' as usual"
             function-symbol)
           (let ((yas/fallback-behavior nil))
             (unless (and (interactive-p)
                          (yas/expand))
               ad-do-it)))))

(yas/advise-indent-function 'cdlatex-tab)
(yas/advise-indent-function 'org-cycle)
(yas/advise-indent-function 'org-try-cdlatex-tab)
(add-hook 'org-mode-hook 'yas/minor-mode-on)
;-------- Some little personal script for latex --------------
(fset 'beamer-frame-compile
   (lambda (&optional arg) "Keyboard
   macro." (interactive "p") (kmacro-exec-ring-item (quote ([18
   92 98 101 103 105 110 123 102 114 97 109 101 125 67108896 19
   92 101 110 100 123 102 114 97 109 101 125 3 18 108 97 116 101
   120 return] 0 "%d")) arg)))


(provide 'latexmodel_linux)

